package com.yoga.admin.api.web;

import com.yoga.admin.api.annotation.LoginUser;
import com.yoga.admin.api.annotation.UserTokenManage;
import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.appexception.YogaResult;
import com.yoga.common.bo.YogaAdminBO;
import com.yoga.manager.admin.AdminYogaAdminManager;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created by hdx on 2020/9/28.
 */
@RestController
@RequestMapping("${basePath}")
public class AdminYogaAdminController {
    @Resource
    private AdminYogaAdminManager adminYogaAdminManager;

    @Resource(name="stringRedisTemplate")
    private ValueOperations<String,String> vops;

    @PostMapping("/register")
    public YogaResult<YogaAdminBO> adminRegister(@RequestBody YogaAdminBO yogaAdminBO) throws AppException {
        YogaAdminBO result = adminYogaAdminManager.adminRegister(yogaAdminBO);

        return new YogaResult<>(result);
    }

    @PostMapping("/login")
    public YogaResult<YogaAdminBO> login(@RequestBody YogaAdminBO yogaAdminBO) throws AppException {

        YogaAdminBO result = adminYogaAdminManager.login(yogaAdminBO);
        UserTokenManage.getInstance(vops).setLoginUser(result);

        return new YogaResult<>(result);
    }

    @PostMapping("/exit")
    public YogaResult<Boolean> exit(@LoginUser YogaAdminBO yogaAdminBO) throws AppException{
        UserTokenManage.getInstance(vops).delete(yogaAdminBO.getYogaAdminId());
        return new YogaResult<>(true);
    }

    @GetMapping("/getById")
    public YogaResult<YogaAdminBO> findById(@LoginUser YogaAdminBO yogaAdminBO) throws AppException {
        YogaAdminBO result = adminYogaAdminManager.getById(yogaAdminBO.getYogaAdminId());

        return new YogaResult<>(result);
    }

    @PostMapping("/update")
    public YogaResult<Boolean> updateData(@LoginUser YogaAdminBO yogaAdminBO, @RequestBody YogaAdminBO adminBO) throws AppException {
        if (adminBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        adminBO.setYogaAdminId(yogaAdminBO.getYogaAdminId());
        Boolean result = adminYogaAdminManager.updateById(adminBO);

        return new YogaResult<>(result);
    }
}
